package com.mmall.controller.backend;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.Category;
import com.mmall.pojo.Product;
import com.mmall.service.ICategoryService;
import com.mmall.service.IFileService;
import com.mmall.service.IProductService;
import com.mmall.util.DateTimeUtil;
import com.mmall.util.PojoToVo;
import com.mmall.util.PropertiesUtil;
import com.mmall.vo.ProductDetailVo;
import com.mmall.vo.ProductListVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by geely
 */

@Controller
@RequestMapping("/manage/product")
public class ProductManageController {

    @Autowired
    private IProductService iProductService;
    @Autowired
    private IFileService iFileService;
    @Autowired
    private ICategoryService iCategoryService;


    @RequestMapping("save")
    @ResponseBody
    public ServerResponse productSave(Product product){

        //登录验证交给过滤器
        //填充我们增加产品的业务逻辑
        return iProductService.saveOrUpdateProduct(product);

    }

    @RequestMapping("set_sale_status")
    @ResponseBody
    public ServerResponse setSaleStatus(Integer productId, Integer status){

        //登录验证交给过滤器
        return iProductService.setSaleStatus(productId,status);

    }

    @RequestMapping("detail")
    @ResponseBody
    public ServerResponse getDetail(Integer productId){

        //登录验证交给过滤器
        //填充业务
        ServerResponse<Product> productServerResponse = iProductService.manageProductDetail(productId);
        Product product=productServerResponse.getData();
        ServerResponse<Category> categoryServerResponse = iCategoryService.selectByPrimaryKey(product.getCategoryId());
        Category category = categoryServerResponse.getData();

        ProductDetailVo productDetailVo =PojoToVo.assembleProductDetailVo(product,category.getParentId());
        return ServerResponse.createBySuccess(productDetailVo);


    }

    @RequestMapping("list")
    @ResponseBody
    public ServerResponse getList(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){

        //登录验证交给过滤器
        //填充业务
        ServerResponse<PageInfo> pageInfoServerResponse = iProductService.getProductList(pageNum, pageSize);
        PageInfo pageInfo = pageInfoServerResponse.getData();
        List<Product> productList = pageInfo.getList();
        List<ProductDetailVo> productDetailVoList=new ArrayList<ProductDetailVo>();
        for (Product product:productList){
            ServerResponse<Category> categoryServerResponse = iCategoryService.selectByPrimaryKey(product.getCategoryId());
            Category category = categoryServerResponse.getData();
            ProductDetailVo productDetailVo = PojoToVo.assembleProductDetailVo(product,category.getParentId());
            productDetailVoList.add(productDetailVo);
        }
        pageInfo.setList(productDetailVoList);
        return pageInfoServerResponse;

    }

    @RequestMapping("search")
    @ResponseBody
    public ServerResponse productSearch(String productName, Integer productId, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){

        //登录验证交给过滤器
        //填充业务
        ServerResponse<PageInfo> productResponse = iProductService.searchProduct(productName, productId, pageNum, pageSize);
        PageInfo pageInfo = productResponse.getData();
        List<Product> productList = pageInfo.getList();
        List<ProductListVo> productListVoList = Lists.newArrayList();
        for(Product productItem : productList){
            ProductListVo productListVo = PojoToVo.assembleProductListVo(productItem);
            productListVoList.add(productListVo);
        }
        pageInfo.setList(productListVoList);
        return productResponse;

    }

    @RequestMapping("upload")
    @ResponseBody
    public ServerResponse upload(@RequestParam(value = "upload_file",required = false) MultipartFile file, HttpServletRequest request){

        //登录验证交给过滤器
        String path = request.getSession().getServletContext().getRealPath("upload");
        ServerResponse<String> upload = iFileService.upload(file, path);
        if (upload.isSuccess()) {
            String targetFileName =upload.getData();
            String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;

            Map fileMap = Maps.newHashMap();
            fileMap.put("uri",targetFileName);
            fileMap.put("url",url);
            return ServerResponse.createBySuccess(fileMap);

        }
        return upload;
    }


    @RequestMapping("richtext_img_upload")
    @ResponseBody
    public Map richtextImgUpload( @RequestParam(value = "upload_file",required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response){

        Map resultMap = Maps.newHashMap();
        //登录验证交给过滤器
        String path = request.getSession().getServletContext().getRealPath("upload");
        ServerResponse<String> upload = iFileService.upload(file, path);
        if(upload.isSuccess()){
            String targetFileName =upload.getData();
            String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;
            resultMap.put("success",true);
            resultMap.put("msg","上传成功");
            resultMap.put("file_path",url);
            response.addHeader("Access-Control-Allow-Headers","X-File-Name");
            return resultMap;
        }
        resultMap.put("success",false);
        resultMap.put("msg","上传失败");
        return resultMap;


    }





























}
